﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reticulum
{
    public partial class Updata : Form
    {
        public Updata()
        {
            InitializeComponent();
        }

        private void Updata_Load(object sender, EventArgs e)
        {
            //textBox1.Text = null;//昵称
            //comboBox1.Text = null;//性别
            //dateTimePicker1.Text = null;//生日
            //comboBox2.Text = null;//血型
            //textBox3.Text = null;//签名
            //textBox4.Text = null;//职业
            //textBox5.Text = null;//公司
            //textBox6.Text = null;//所在地区
            //textBox7.Text = null;//家乡
            //textBox8.Text = null;//电话
            //textBox9.Text = null;//邮箱
            //textBox10.Text = null;//个人说明
            //textBox2.Text = null;//账号
            string sql1 = "select UserAccount from Users";

            DataSet dst = DBHelps.Adapter(sql1);

            dataGridView1.AutoGenerateColumns = false;

            dataGridView1.DataSource = dst.Tables[0];
        }

        public static string UserName, UserAccount, UserPwd, UserPhone,
            MySignature, Occupational, Company, Area, Hometown, Email,
            MyDescription, Birthday, Sex, BloodGroup;

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            bool sult = false;
            string selsect = string.Format("select UserAccount,UserName,UserPwd,UserPhone,MySignature,Occupational,Company,Area," +
                "Hometown,Email,MyDescription,Birthday,Sex,BloodGroup from Users where UserAccount='{0}'", dataGridView1.CurrentCell.Value.ToString());//判断搜索的账号

            while (DBHelps.Select(selsect).Read())//循环查找信息
            {
                UserName = DBHelps.Select(selsect)["UserName"].ToString();
                UserPwd = DBHelps.Select(selsect)["UserPwd"].ToString();
                UserPhone = DBHelps.Select(selsect)["UserPhone"].ToString();
                MySignature = DBHelps.Select(selsect)["MySignature"].ToString();
                Occupational = DBHelps.Select(selsect)["Occupational"].ToString();
                Company = DBHelps.Select(selsect)["Company"].ToString();
                Area = DBHelps.Select(selsect)["Area"].ToString();
                Hometown = DBHelps.Select(selsect)["Hometown"].ToString();
                Email = DBHelps.Select(selsect)["Email"].ToString();
                MyDescription = DBHelps.Select(selsect)["MyDescription"].ToString();
                Birthday = DBHelps.Select(selsect)["Birthday"].ToString();
                Sex = DBHelps.Select(selsect)["Sex"].ToString();
                BloodGroup = DBHelps.Select(selsect)["BloodGroup"].ToString();
                UserAccount = DBHelps.Select(selsect)["UserAccount"].ToString();
                sult = true;
            }
            if (sult)
            {
                //如果找到了账号再进行赋值
                textBox1.Text = UserName;//昵称
                comboBox1.Text = Sex;//性别
                dateTimePicker1.Text = Birthday;//生日
                comboBox2.Text = BloodGroup;//血型
                textBox3.Text = MySignature;//签名
                textBox4.Text = Occupational;//职业
                textBox5.Text = Company;//公司
                textBox6.Text = Area;//所在地区
                textBox7.Text = Hometown;//家乡
                textBox8.Text = UserPhone;//电话
                textBox9.Text = Email;//邮箱
                textBox10.Text = MyDescription;//个人说明
                textBox2.Text = UserAccount;//账号
                MessageBox.Show("搜索成功！");
            }
            else
            {
                MessageBox.Show("没有找到该账号！");
            }
            DBHelps.Select(selsect).Close();
            DBHelps.reader = null;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Updata_Load(sender, e);
        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            if (textBox11.Text=="")
            {
                label1.Visible = true;
            }
            else
            {
                label1.Visible = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)//搜索
        {
            bool sult = false;
            string selsect = string.Format("select UserAccount,UserName,UserPwd,UserPhone,MySignature,Occupational,Company,Area," +
                "Hometown,Email,MyDescription,Birthday,Sex,BloodGroup from Users where UserAccount='{0}'", textBox11.Text);//判断搜索的账号

            while (DBHelps.Select(selsect).Read())//循环查找信息
            {
                UserName = DBHelps.Select(selsect)["UserName"].ToString();
                UserPwd = DBHelps.Select(selsect)["UserPwd"].ToString();
                UserPhone = DBHelps.Select(selsect)["UserPhone"].ToString();
                MySignature = DBHelps.Select(selsect)["MySignature"].ToString();
                Occupational = DBHelps.Select(selsect)["Occupational"].ToString();
                Company = DBHelps.Select(selsect)["Company"].ToString();
                Area = DBHelps.Select(selsect)["Area"].ToString();
                Hometown = DBHelps.Select(selsect)["Hometown"].ToString();
                Email = DBHelps.Select(selsect)["Email"].ToString();
                MyDescription = DBHelps.Select(selsect)["MyDescription"].ToString();
                Birthday = DBHelps.Select(selsect)["Birthday"].ToString();
                Sex = DBHelps.Select(selsect)["Sex"].ToString();
                BloodGroup = DBHelps.Select(selsect)["BloodGroup"].ToString();
                UserAccount = DBHelps.Select(selsect)["UserAccount"].ToString();
                sult = true;
            }
            if (sult)
            {
                //如果找到了账号再进行赋值
                textBox1.Text = UserName;//昵称
                comboBox1.Text = Sex;//性别
                dateTimePicker1.Text = Birthday;//生日
                comboBox2.Text = BloodGroup;//血型
                textBox3.Text = MySignature;//签名
                textBox4.Text = Occupational;//职业
                textBox5.Text = Company;//公司
                textBox6.Text = Area;//所在地区
                textBox7.Text = Hometown;//家乡
                textBox8.Text = UserPhone;//电话
                textBox9.Text = Email;//邮箱
                textBox10.Text = MyDescription;//个人说明
                textBox2.Text = UserAccount;//账号
                MessageBox.Show("搜索成功！");
            }
            else
            {
                MessageBox.Show("没有找到该账号！");
            }
            DBHelps.Select(selsect).Close();
            DBHelps.reader = null;
            
        }

        private void button2_Click(object sender, EventArgs e)
        {

            //邮箱 验证通过-----------------------
            bool Verify = false;
            int Pass = textBox9.Text.IndexOf("@");
            if (textBox9.Text != "" && Pass == -1)
            {
                textBox9.ForeColor = Color.Red;
            }
            else { Verify = true; }
            //------------------------------------


            if (Verify)
            {
                string sql = string.Format("update Users set UserName='{0}',Sex='{1}',Birthday='{2}',BloodGroup='{3}',MySignature='{4}',Occupational='{5}',Company" +
                "='{6}',Area='{7}',Hometown='{8}',Email='{9}',MyDescription='{10}',UserPhone='{11}',UserAccount='{12}'" + " where UserAccount='{13}'",
                textBox1.Text, comboBox1.Text, dateTimePicker1.Text, comboBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text,
                 textBox6.Text, textBox7.Text, textBox9.Text, textBox10.Text, textBox8.Text,textBox2.Text ,UserAccount);

                bool Results = DBHelps.Command(sql);

                if (Results)
                { MessageBox.Show("保存成功"); }
                else { MessageBox.Show("保存失败"); }
            }
            else
            {
                MessageBox.Show("请输入正确格式");
            }
        }
    }
}
